

class Description(object):
    def __init__(self):
        self._name=""
        self.doc=None
        self.objectType=""
        self.valueType=""
        self.value=""
        self.level=0
        self.parent=None
        self.line=""
        self.childs=[]
        self.subDescription=[]
        self.block=False
        self.forceParent=False
        self.access=""
        self.override=False
        self.constructor=False
        self.replaceDesc=None
        self.hasSuper=False
        self.static=False
        self.scanned=False
        self.rename=None
        self.path=""
        self.asFile=None
        self.use=0
        self.temp=0
        
    @property
    def name(self):
        return self._name

    @name.setter
    def name(self, val):
        self._name=val
    
    def addChild(self,child):
        self.childs.append(child)
        child.parent=self
        
    def getChilds(self): 
        return self.childs
    
    def findSubDescription(self,name):
        for child in self.subDescription:
            if type(child)!=str and child.name==name:
                return child
        return None
    
    def findParentByObjectType(self,type_):
        if self.objectType==type_:
            return self
        elif self.parent:
            return self.parent.findParentByObjectType(type_)
        return None
    
    def findChildsByObjectType(self,type_):
        ret=[]
        print "findChildsByObjectType"
        for child in self.childs:
            if child.objectType==type_:
                ret.append(child)
            ret+=child.findChildsByObjectType(type_)
        return ret
    
    def findChildByObjectType(self,type_):
        ret=None
        for child in self.childs:
            if child.objectType==type_:
                return child
            else:
                ret=child.findChildByObjectType(type_)
                if ret:
                    return ret
        return ret    
        
    def findChild(self,name):
        for child in self.childs:
            if(child.name==name):
                return child
        return None
    
    def findRenameChild(self,name):
        for child in self.childs:
            if(child.rename==name):
                return child
        return None
    
    def findChildAndValueChild(self,name):
        ret=self.findChild(name)# or self.findRenameChild(name)
        if not ret and self.valueType and isinstance(self.valueType, Description):
            return self.valueType.findChildAndValueChild(name)
        return ret
    
    def findArg(self,name):
        for arg in self.args:
            if(arg.name==name):
                return arg
        return None
    

    def changeLevel(self,add):
        self.level+=add
        for child in self.childs:
            child.level+add
            child.changeLevel(add)
    
    def getName(self):
        from core.config import Config
        from core.js import getShrinkName
        if Config.COMPRESSION>=4 and not self.rename and (self.objectType=="property" or self.objectType=="method"):
            self.rename=getShrinkName(self.name,self.parent,self.parent.name,"")
        if Config.COMPRESSION>=3 and not self.rename and self.objectType=="class":
            self.rename=getShrinkName(self.name,self.parent,"world","$")
        if Config.COMPRESSION>=2 and not self.rename and self.access=="private":
            self.rename=getShrinkName(self.name,self.parent,self.parent.name,"")
        if self.rename:
            return self.rename
        else:
            return self.name
    
    def __str__(self):
        return "Description =>"+self.name + " ["+self.objectType+"] "+self.line
    
    def __repr__(self):
        return "Description =>"+self.name + " ["+self.objectType+"] "+self.line
    
class Line:
    def __init__(self,line="",level=0,desc=None):
        self.line=line
        self.close=False
        self.level=level
        self.desc=desc
        self.doc=[]
        self.readChild=False
        
    def __repr__(self):
        return "Line =>"+self.line